package org.example.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.util.ArrayList;
import java.util.List;

// 数据类
class Data {
    @ExcelProperty("序号")
    private Integer id;

    @ExcelProperty("姓名")
    private String name;

    public Data(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

// 自定义监听器实现序号自增


public class EasyExcelIndexExample {
    public static void main(String[] args) {
        String fileName = "index_example.xlsx";

        // 模拟数据
        List<Data> dataList = new ArrayList<>();
        dataList.add(new Data(null, "张三"));
        dataList.add(new Data(null, "李四"));
        dataList.add(new Data(null, "王五"));

        // 写入 Excel
        EasyExcel.write(fileName, Data.class)
               .registerWriteHandler(new IndexCellWriteHandler())
               .sheet("Sheet1")
               .doWrite(dataList);
    }
}